(function ($) {

    /* 生成返回顶端 **/
    $('body').append('\
    <div class="rollbar"><ul>' +
        '<li><a href="#top" class="scroll-to"><i class="fa fa-angle-up"></i></a><h6>去顶部<i></i></h6></li>\
        </ul></div>\
    ');

    /** 平滑滚动锚链接 **/
    $(window.document).on('click', '.scroll-to', function (){
        var toId = $(this).attr('href');
        $.scrollTo(toId,800);
        return false;
    });

    /* 生成手机端菜单 **/
    if ($.clientDevice() != 'pc') {
        $('#main-nav').clone().attr({id:'phone-main-menu',class:''}).appendTo('body');
        $('#show-main-menu').sidr({
            name: 'phone-main-menu',
            speed: 500,
            source: '#phone-main-menu',
            side: 'right',
            onOpenEnd: function () {
                $('body #show-main-menu').addClass('active');
                $('body #show-main-menu i').attr('class','fa fa-close');
            },
            onCloseEnd: function () {
                $('body #show-main-menu').removeClass('active');
                $('#show-main-menu i').attr('class','fa fa-bars');
            }
        });
    }

    /* 异步加载图片 **/
    if ($('.thumb:first').data('src') || $('.widget_ui_posts .thumb:first').data('src') || $('.wp-smiley:first').data('src') || $('.avatar:first').data('src')) {
        $('.avatar').lazyload({
            data_attribute: 'src',
            placeholder: '/modules/user/face/avatar3.png',
            threshold: 400
        });

        $('.thumb').lazyload({
            data_attribute: 'src',
            placeholder: '/themes/itdashu/images/banner/270x165-7.png',
            threshold: 400
        });
    }

    /* 手机端用户登录菜单 **/
    if($(window).width() <= 480){
        $('#phone-user').click(function () {
            $('header .right .user-info').fadeToggle(100);
        });
        $('header .right .user-info').focusout(function () {
            $(this).fadeOut(100);
        });
    }

    /* ajax model 异步记载ajax-model*/
    var myModal = $('#myModal');
    $('body').on('click', '[ajax-model]', function () {
        var ajaxModel = $(this).attr('ajax-model');
        var loadedAjaxModel = myModal.attr('loaded-url');
        if (ajaxModel == loadedAjaxModel) {
            myModal.modal('show');
        } else {
            $.get(ajaxModel, function (data) {
                myModal.html(data).attr('loaded-url', ajaxModel);
                myModal.modal('show');
            });
        }
        return false;
    });
    // ajax json notice
    $('[ajax-notice]').click(function () {
        var ajaxModel = $(this).attr('ajax-notice');

        $.ajax({
            type: "get",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: ajaxModel, //这里是网址
            success: function (data) {
                if (typeof(data.notice) == "undefined" || typeof(data.state) == "undefined") {
                    toastr.warning('请求失败，请检查网络');
                } else {
                    toastr.success(data.notice);
                }
            },
            timeout: 1000,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                toastr.warning('请求失败，请检查网络，或联系管理员');
            }
        });
        return false;
    });


    /** 滚动监听**/
    $('.block-fixed').each(function () {
        var currentOffset = $(this).offset();
        var width = $(this).width();
        $(this).attr('offsetTop',currentOffset.top).css('width',width);
    });
    var scroller = $('.rollbar')
    $(window).scroll(function () {
        var h = document.documentElement.scrollTop + document.body.scrollTop
        //alert(aa.top);
        $('.block-fixed').each(function () {
            var currentOffset = $(this).attr('offsetTop');
            if(currentOffset <= h){
                $(this).css({
                    'position':'fixed',
                    'top':8,
                    'bottom':0,
                    'overflow-y':'scroll'
                })
            }else {
                $(this).css({
                    'position':'static'
                })
            }
        });

        /**
         if (_fix && h > 0 && _wid > 720) {
            jsui.bd.addClass('nav-fixed')
        } else {
            jsui.bd.removeClass('nav-fixed')
        }
         **/

        h > 200 ? scroller.fadeIn() : scroller.fadeOut();
    })

})(jQuery);

//百度统计
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?1abeb845aac1917457e6ae8b91da4f29";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
